﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：PublicRedisManager.cs
// 功能描述：TongTongMall.MallWeb.Application
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.Linq;
using System;
using System.Threading.Tasks;
using TEC.Public.Component.OpenPlat.Tencent.WeiXin;
using TongTongMall.Dependecy;
using TongTongMall.Domain.Services;
using TongTongMall.Events;
using TongTongMall.MallWeb.Core.Events.EventDatas;
using TongTongMall.Oauth2._0.Models;
using TongTongMall.Redis.Cache;
using TongTongMall.Redis.Cache.Configuration;
using TongTongMall.Runtime.Session;


namespace TongTongMall.MallWeb.Core.Components.Services
{
    public class PublicRedisManager : IPublicCacheService, IDomainService
    {
        private IRedisCache _redisCache;

        public IEventBus EventBus { get; set; } = NullEventBus.Instance;

        public IRedisConfiguration RedisConfigurationManager { get; set; } = RedisConfiguration.Default;

        public PublicRedisManager(IRedisCache _redisCache)
        {
            this._redisCache = _redisCache;
        }



        #region 微信授权Token
        public async Task<TType> GetWX_AccessToken<TType>(string appid) where TType : AccessToken
        {
            var userDetail = await _redisCache.GetOrDefaultAsync(GroupRedisKey("__WX_AccessToken", $"{appid}"));
            return (TType)userDetail ?? default(TType);
        }

        public Task SetWX_AccessToken<TType>(TType accesstoken, TimeSpan expireTime, string appid) where TType : AccessToken
        {
            if (accesstoken == null) throw new TongTongMallException("the accesstoken is null！");
            EventBus.Trigger(typeof(RedisParameterEventData),
                this,
                new RedisParameterEventData { Keys = GroupRedisKey("__WX_AccessToken", $"{appid}"), Data = accesstoken, Expire = expireTime }
                );
            return Task.FromResult(0);
        }
        public Task RemoveWX_AccessToken(string appid)
        {
            EventBus.Trigger(typeof(RedisRemoveEventData), this, new RedisRemoveEventData { Keys = GroupRedisKey("__WX_AccessToken", $"{appid}") });
            return Task.FromResult(0);
        }
        #endregion

        #region 微信授权JSTicket
        public async Task<TType> GetWX_JsApiTicket<TType>(string appid) where TType : JsApiTicket
        {
            var userDetail = await _redisCache.GetOrDefaultAsync(GroupRedisKey("__WX_JsApiTicket", $"{appid}"));
            return (TType)userDetail ?? default(TType);
        }

        public Task SetWX_JsApiTicket<TType>(TType accesstoken, TimeSpan expireTime, string appid) where TType : JsApiTicket
        {
            if (accesstoken == null) throw new TongTongMallException("the accesstoken is null！");
            EventBus.Trigger(typeof(RedisParameterEventData),
                this,
                new RedisParameterEventData { Keys = GroupRedisKey("__WX_JsApiTicket", $"{appid}"), Data = accesstoken, Expire = expireTime }
                );
            return Task.FromResult(0);
        }
        public Task RemoveWX_JsApiTicket(string appid)
        {
            EventBus.Trigger(typeof(RedisRemoveEventData), this, new RedisRemoveEventData { Keys = GroupRedisKey("__WX_JsApiTicket", $"{appid}") });
            return Task.FromResult(0);
        }
        #endregion

        /// <summary>
        /// 组合redis的value
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        protected string GroupRedisKey(params string[] values)
        {
            return RedisConfigurationManager.GroupKeys(values);
        }
    }
}
